<?php
/*
 * 搜索页面的文件
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: f6923817f82b00a18baa602fa6ddb5b2d53cb3ef $
 */
defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE . '/' . $module . '/common.inc.php';
// init param
$l  = (!isset($l) || empty($l)) ? 0 : intval($l);
$p  = (!isset($p) || empty($p)) ? 0 : intval($p);
$e  = (!isset($e) || empty($e)) ? 0 : intval($e);
$m  = (!isset($m) || empty($m)) ? 0 : intval($m);
$g  = (!isset($g) || empty($g)) ? 0 : intval($g);
$o  = (!isset($o) || empty($o)) ? 0 : intval($o);
$x  = (!isset($x)) ? '' : strval($x);
$kw = (!isset($kw)) ? '' : strval($kw);
if ($kw == '输入车型...') $kw = '';
$RANK   = AutoCategory::getCache(AutoCategory::TYPE_RANK);
$LEVEL  = AutoCategory::getCache(AutoCategory::TYPE_LEVEL);
$search = new AutoSearchModule();
$search->addSel('l', $l);
$search->addSel('p', $p);
$search->addSel('e', $e);
$search->addSel('m', $m);
$search->addSel('g', $g);
$search->addSel('o', $o);
$search->addSel('kw', $kw);

// 显示可以选择的参数
// ----显示公共参数
$params = $search->getParams();
if ($l) {
	$levelSelected  = $params['level']['cache'][$l];
	$levelParentids = explode(',', $levelSelected['arrparentid'] . ',' . $l);
	array_shift($levelParentids);
	$levelParents = array();
	foreach ($levelParentids as $k) {
		$levelParents[] = $params['level']['cache'][$k];
	}
}


$modelSql    = $search->getParamSql();
$modelExtSql = '';
// ----显示私有参数
if ($l) {
	//生成私有参数
	$extParams = $search->extParams($l);
	$lLocation = AutoCategory::getLocation($l, $LEVEL);
	//假如点击 类型不限，清空私有条件
	if (isset($x)) {
		if (strlen($x) !== count($extParams)) {
			$ex = $x = str_repeat('0', count($extParams));
		} else {
			$ex = $x;
		}
	} else {
		$ex = str_repeat('0', count($extParams));
	}

	//将私有属性存入数组，用于参数显示
	$extCheckboxes = array();
	$extListProps  = array();
	$extIndex      = 0;
	$extSql        = array();
	foreach ($extParams as $k => $val) {
		$values = AutoField::handleOptionValue($val['option_value']);
		if ($values[2] == '选装') {
			// checkbox value
			$name                                   = $val['name']; //地址栏接收赋值的变量名
			$extCheckboxes[$val['title']]['_set']   = substr_replace($ex, '1', $k, 1);
			$extCheckboxes[$val['title']]['_empty'] = substr_replace($ex, '0', $k, 1);
			$sel                                    = substr($ex, $k, 1);
			$extCheckboxes[$val['title']]['_sel']   = $sel;
			$extCheckboxes[$val['title']]['_nsel']  = !$sel;
			$extCheckboxes[$val['title']][$name]    = $values;
			if ($sel) {
				$extSql[] = 'em.' . $name . '=' . $sel;
			}

		} else {
			// list values
			if ($values[0] == '请选择') unset($values[0]);

			$name = $val['name']; //地址栏接收赋值的变量名
			$extListProps[$val['title']]['_set'] = array();

			foreach ($values as $ck => $cv) {
				$str                                   = substr_replace($ex, $ck, $k, 1);
				$extListProps[$val['title']]['_set'][] = $str;
				$extListProps[$val['title']]['_cur'][] = substr($str, $k, 1);
			}
			$sel = substr($ex, $k, 1);

			$extListProps[$val['title']]['_sel']   = $sel;
			$extListProps[$val['title']]['_empty'] = substr_replace($ex, '0', $k, 1);
			$extListProps[$val['title']][$name]    = $values;
			if ($sel) {
				$extSql[] = 'em.' . $name . '=' . $sel;
			}
		}
	}

	if ($extSql) {
		$modelExtSql = implode(' and ', $extSql);
	}
}

// 1. 获取参数值
// 2. 根据相关的参数值生成SQL语句
// 3. 获取车系数量设置分页
$search->getNums($modelSql, $modelExtSql);
// 4. 获取需要显示的页面内容
$search->getCarids($modelSql, $modelExtSql);
//----获取需要显示的车系数量
//----获取需要显示的车款的数量, 然后进行插入联合
$carInfos = $search->getCarInfos();

$modelInfos = $search->getModelInfos($modelSql, $modelExtSql);

$items = $search->mergeCarsAndExts($carInfos, $modelInfos);

// 5. 加载模版
include template('search', 'auto');













